package com.everydayflash.flar.mustang {
	import com.as3dmod.ModifierStack;
	import com.as3dmod.modifiers.Pivot;
	import com.as3dmod.modifiers.Wheel;
	import com.as3dmod.plugins.pv3d.LibraryPv3d;
	import com.everydayflash.math.Vector2;
	
	import org.papervision3d.core.proto.MaterialObject3D;
	import org.papervision3d.events.FileLoadEvent;
	import org.papervision3d.materials.BitmapMaterial;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.parsers.DAE;
	
	import flash.display.BitmapData;
	import flash.geom.ColorTransform;	

	/**
	 * @author bartekd
	 */
	public class Mustang extends DAE implements ICar {

		private var frontWheelLeft:DisplayObject3D;
		private var frontWheelRight:DisplayObject3D;

		private var rearWheelLeft:DisplayObject3D;
		private var rearWheelRight:DisplayObject3D;

		private var leftWheel:Wheel;
		private var rightWheel:Wheel;

		private var fwl:ModifierStack;
		private var fwr:ModifierStack;
		private var scaleFactor:Number = 10;
		private var body:DisplayObject3D;

		private var rearLightLeft:DisplayObject3D;
		private var rearLightRight:DisplayObject3D;

		[Embed (source="assets/rearlights.jpg")]
		private var RearLightsTexture:Class;
		private var rearLightsTemplate:BitmapData;
		private var rearLightsMaterial:BitmapMaterial;
		private var _base:Number;
		private var maxBank:Number = 0.025;
		public function Mustang() {
			super(false, "mustang", false);
			load("mustang/mustang.dae");
			addEventListener(FileLoadEvent.LOAD_COMPLETE, onMustang);
		}

		public function get MAX_TURN():Number {
			return 30 / 180 * Math.PI;
		}

		private function onMustang(event:FileLoadEvent):void {
			scale = scaleFactor;
			
			rearLightLeft = getChildByName("rearlight_l", true);
			rearLightRight = getChildByName("rearlight_r", true);
			
			rearLightsTemplate = new RearLightsTexture().bitmapData;
			
			rearLightsMaterial = new BitmapMaterial(rearLightsTemplate.clone());
			rearLightLeft.material = rearLightsMaterial;			rearLightRight.material = rearLightsMaterial;
			
			frontWheelLeft = getChildByName("frontwheel_l", true);
			frontWheelRight = getChildByName("frontwheel_r", true);
			
			rearWheelLeft = getChildByName("rearwheel_l", true);
			rearWheelRight = getChildByName("rearwheel_r", true);

			body = getChildByName("car", true);
			
//			var b:DisplayObject3D = getChildByName("body", true);
//			b.material = new WireframeMaterial(0xff0000);
			
			body.y += .07;
			frontWheelLeft.x -= .05;			frontWheelRight.x -= .05;

			fwl = new ModifierStack(new LibraryPv3d(), frontWheelLeft);
			var fwlp:Pivot = new Pivot();
			fwl.addModifier(fwlp);
			fwlp.setMeshCenter();
			//			fwlp.pivot.z -= fwlp.size /2;
			fwl.collapse();
			leftWheel = new Wheel();
			fwl.addModifier(leftWheel);
						_base = y - frontWheelLeft.y + fwl.meshInfo.height / 2;

			fwr = new ModifierStack(new LibraryPv3d(), frontWheelRight);
			var fwrp:Pivot = new Pivot();
			fwr.addModifier(fwrp);
			fwrp.setMeshCenter();
			//			fwrp.pivot.z += fwrp.size /2;
			fwr.collapse();
			rightWheel = new Wheel();
			fwr.addModifier(rightWheel);
			
			var rwl:ModifierStack = new ModifierStack(new LibraryPv3d(), rearWheelLeft);
			var rwlp:Pivot = new Pivot();
			rwl.addModifier(rwlp);
			rwlp.setMeshCenter();
			rwl.collapse();
			
			var rwr:ModifierStack = new ModifierStack(new LibraryPv3d(), rearWheelRight);
			var rwrp:Pivot = new Pivot();
			rwr.addModifier(rwrp);
			rwrp.setMeshCenter();
			rwr.collapse();
			
//			dispatchEvent(event);

//			rotationX = 90;
		}

		public function drive(v:Vector2, orientation:Number, direction:Number, brake:Boolean = false, handBrake:Boolean = false):void {
			// not very accurate I think
			var a2:Number = v.magnitude * direction * leftWheel.radius;
			
			leftWheel.speed = a2;
			rightWheel.speed = a2;
			fwl.apply();
			fwr.apply();
			
			rearWheelLeft.roll(a2);
			rearWheelRight.roll(a2);

			x += v.x * scaleFactor;			z += v.y * -scaleFactor;
			
			if(brake && v.magnitude > .5) body.rotationZ = -.016;
			else if(v.magnitude > 0) body.rotationZ *= 0.80;
			else body.rotationZ *= 0.60;
			
			if(brake) rearLightsMaterial.bitmap.draw(rearLightsTemplate, null, new ColorTransform(1.6));			else rearLightsMaterial.bitmap.draw(rearLightsTemplate, null);
			
			rotationY = orientation;
		}

		public function turn(angle:Number, speed:Number):void {
			leftWheel.turn = angle;
			rightWheel.turn = angle;
			
			var rx:Number = angle / 16 * speed;
			rx = Math.min(rx, maxBank);			rx = Math.max(rx, -maxBank);

//			body.rotationX = rx;
		}

		public function get base():Number {
			return -_base;
		}
		
		public function get _fwl():ModifierStack {
			return fwl;
		}
		
		public function set _fwl(fwl:ModifierStack):void {
			this.fwl = fwl;
		}
	}
}
